package cn.itsource.ymcc.service.impl;

import cn.itsource.ymcc.domain.CourseType;
import cn.itsource.ymcc.dto.CrumbsListDto;
import cn.itsource.ymcc.mapper.CourseTypeMapper;
import cn.itsource.ymcc.service.ICourseTypeService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程目录 服务实现类
 * </p>
 *
 * @author Eric Chen
 * @since 2022-07-11
 */
@Service
public class CourseTypeServiceImpl extends ServiceImpl<CourseTypeMapper, CourseType> implements ICourseTypeService {
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    // 级联树
    public List<CourseType> toTreeData() {
        List<CourseType> courseTypes = selectList(null);
        Map<Long, CourseType> courseTypeMap = courseTypes.stream().collect(Collectors.toMap(CourseType::getId, CourseType -> CourseType));
        List<CourseType> list = new ArrayList<>();
        courseTypes.forEach(courseType -> {
            if (courseType.getPid() == null || courseType.getPid() == 0L){
                list.add(courseType);
            }else {
                CourseType courseType1 = courseTypeMap.get(courseType.getPid());
                courseType1.getChildren().add(courseType);
            }
        });
        System.out.println(list.toString());
        return list;
    }
    // 缓存实现
    @Override
    @Cacheable(cacheNames="CourseType",key="'treeData'")   // books::book1 查询储存redis
    public List<CourseType> treeData() {
//        List<CourseType> list = (List<CourseType>)redisTemplate.opsForValue().get("treeData");
//        if (list == null){
//            list = toTreeData();
//            redisTemplate.opsForValue().set("treeData",list,12, TimeUnit.HOURS);
//        }
//        return list;
        return toTreeData();
    }



    // 数据更改后删除缓存重新获取
    @Override
    @CacheEvict(cacheNames="CourseType",key="'treeData'") //删除redis
    public boolean insert(CourseType entity) {
        boolean insert = super.insert(entity);
        //if (insert)redisTemplate.delete("treeData");
        return insert;
    }

    // 面包屑
    @Override
    public List<CrumbsListDto> crumbs(Long id) {
        String courseType = selectById(id).getPath();
        String[] split = courseType.split("\\.");
        List<CrumbsListDto> list = new ArrayList<>();
        for (String ids : split) {
            Long aLong = Long.valueOf(ids);
            CourseType courseType1 = selectById(aLong);
            CrumbsListDto dto = new CrumbsListDto();
            dto.setOwnerProductType(courseType1);
            EntityWrapper<CourseType> wrapper = new EntityWrapper<>();
            Long pid = courseType1.getPid();
            wrapper.eq("pid",pid);
            wrapper.ne("id",courseType1.getId());
            List<CourseType> list1 = selectList(wrapper);
            dto.setOtherProductTypes(list1);
            list.add(dto);
        }
        System.out.println(list.toString());
        return list;
    }
}
